<?php
Class Task extends Model
{
	public function __construct()
	{
		parent::Model;
	}
	
	public function Add()
	{
		
		//read into $name and $parentid	
		$parentid=$this->input->post('parentid');
		$name=$this->input->xss_clean($this->input->post('name'));
		$data=array(
		'ParentID'=>$parentid,
		'Name'=>$name,
		'UserID'=>);
		
		try 
		{
			$this->db->insert('taskbasicinfo',$data);
		} catch (Exception $e) 
		{
			return 0;
		}	
		
		return 1;
	}
	
	public function Delete()
	{
		//get id into $id
		
		//relative delete operation
		$sql="delete from taskbasicinfo where ID=$id";
		
		try 
		{
			$this->db->query($sql);
		} catch (Exception $e) 
		{
			return 0;
		}	
		
		return 1;	
	}
	
	public function Modify()
	{
		//read into $id,$parentid,$name
		
		$data=array(
		'ParentID'=>$parentid,
		'Name'=>$name);
		$condition="ID=$id";
		
		try 
		{
			$this->db->update_string("taskbasicinfo",$data,$condition);
		} catch (Exception $e) 
		{
			return 0;
		}	
		
		return 1;		
	}
	
	public function GetAll()
	{
		$sql="select ID,ParentID,Name from taskbasicinfo";
		try 
		{
			$query=$this->db->query($sql);
			$qres=$query->result();
			foreach ($qres as $row)
			{
					$tres[$row->ID]=new TaskInfo();
					$tsk=&$tres[$row->ID];
					$tsk->ID=$row->ID;
					$tsk->Name=$row->Name;
			}
		} catch (Exception $e) 
		{
			return null;
		}	
		
		foreach ($qres as $row)
		{
			if ($row->ParentID==-1)
			{
				$res[$row->ID]=&$tres[$row->ID];
			}
			else
			{
				$tres[$row->ParentID]->Children[$row-ID]=&$tres[$row->ID];
			}
		}
		
		return $res;
	}
	
	public function Get()
	{
		//read id into $id
		
		$sql="select ID,ParentID,Name from taskbasicinfo where ID=$id";
		try 
		{
			$query=$this->db->query($sql);
			foreach ($query->result() as $row)
			{
					$res[$row->ID]=new TaskInfo();
					$tsk=&$res[$row->ID];
					$tsk->ID=$row->ID;
					$tsk->Name=$row->Name;
					$tsk->ParentID=$row->ParentID;
			}
		} catch (Exception $e) 
		{
			return null;
		}	
		
		return $res;
	}
}

Class TaskInfo
{
	public $ID;
	public $Name;
	public $Children;	
	public $ParentID;
}
?>